<template>
  <div style="border: 1px solid #ccc">
    <Editor
      style="height: 100%; overflow-y: hidden"
      v-model="text"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="handleCreated"
    />
  </div>
</template>

<script>
import "@wangeditor/editor/dist/css/style.css"; // 引入 css

import { onBeforeUnmount, ref, shallowRef, watch } from "vue";
import { Editor } from "@wangeditor/editor-for-vue";
export default {
  name: "WEditorInfo",
  components: { Editor },

  props: {
    content: {
      type: String,
      require: true,
    },
  },

  setup(props) {
    // 编辑器实例，必须用 shallowRef
    const editorRef = shallowRef();
    
    const editorConfig = { placeholder: "请输入内容..." };
    editorConfig.readOnly = true;

    const text = ref(props.content);

    watch( () => props.content, (newVal)=> {
      text.value = newVal;
    });

    // 组件销毁时，也及时销毁编辑器
    onBeforeUnmount(() => {
      const editor = editorRef.value;
      if (editor == null) return;
      editor.destroy();
    });

    const handleCreated = (editor) => {
      editorRef.value = editor; // 记录 editor 实例，重要！
    };

    return {
      editorRef,
      mode: "default", // 或 'simple'
      editorConfig,
      handleCreated,

      text,
    };
  },
};
</script>    

<style scoped>
</style>